package com.ethink.order.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.enums.PurchaseReturnOrderReturnStatusEnum;
import com.ethink.order.api.info.PurchaseOrderInfo;
import com.ethink.order.api.request.PurchaseReturnOrderUpdateStatusRequest;
import com.ethink.order.api.response.PurchaseOrderDetailQueryResponse;
import com.ethink.order.api.response.PurchaseReturnDetailResponse;
import com.ethink.order.common.entity.PurchaseReturnDetailOrder;
import com.ethink.order.common.entity.PurchaseReturnOrder;
import com.ethink.order.common.mapper.PurchaseReturnDetailOrderMapper;
import com.ethink.order.common.mapper.PurchaseReturnOrderMapper;
import com.ethink.order.service.PurchaseOrderService;
import com.ethink.order.service.PurchaseReturnOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author default
 * @version 1.0.0
 * @ClassName PurchaseReturnManager.java
 * @Description
 * @createTime 2022年12月23日 13:32:00
 */
@Component
public class PurchaseReturnManager {

    @Autowired
    private PurchaseReturnOrderMapper purchaseReturnOrderMapper;

    @Autowired
    private PurchaseReturnDetailOrderMapper purchaseReturnDetailOrderMapper;

    @Autowired
    private PurchaseReturnOrderService purchaseReturnOrderService;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private OaPayManager oaPayManager;

    /**
     * 采购退审批拒绝
     *
     * @param id
     */
    public Boolean refuse(Long id) {
        PurchaseReturnOrder purchaseReturnOrder = purchaseReturnOrderMapper.selectById(id);
        if (!PurchaseReturnOrderReturnStatusEnum.S2.getIntValue().equals(purchaseReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "非法请求");
        }
        PurchaseReturnOrderUpdateStatusRequest request = new PurchaseReturnOrderUpdateStatusRequest();
        request.setId(id);
        request.setOrderStatus(PurchaseReturnOrderReturnStatusEnum.S1.getIntValue());
        return purchaseReturnOrderService.updateStatus(request);
    }

    public Boolean pass(Long id) {
        Boolean success = purchaseReturnOrderService.pass(id);
        PurchaseReturnOrder purchaseReturnOrder = purchaseReturnOrderMapper.selectById(id);
        PurchaseOrderInfo purchaseOrderInfo = purchaseOrderService.getPurchaseOrderInfo(purchaseReturnOrder.getPurchaseOrderNo());
        PurchaseOrderDetailQueryResponse response = purchaseOrderService.queryPurchaseOrderDetail(purchaseOrderInfo.getId());
        List<PurchaseReturnDetailOrder> purchaseReturnDetailOrders = purchaseReturnDetailOrderMapper.selectList(new LambdaQueryWrapper<PurchaseReturnDetailOrder>()
                .eq(PurchaseReturnDetailOrder::getOrderNo, purchaseReturnOrder.getOrderNo()));
        oaPayManager.returnInsert(response.getMain(), response.getItem(), purchaseReturnOrder, purchaseReturnDetailOrders);
        return success;
    }
}
